package com.liaohuakai.piggybank.remote

import com.liaohuakai.piggybank.remote.dto.WebResponseDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

/**
 * Author: Leo Liao <lliao@midtronics.com>
 * Date: 17/07/2025 17:28
 * Description:
 */

suspend fun <T> Call<WebResponseDto<T>>.resumeWithData(): T {
    return suspendCoroutine {
        enqueue(object : Callback<WebResponseDto<T>> {

            override fun onResponse(
                call: Call<WebResponseDto<T>>,
                response: Response<WebResponseDto<T>>
            ) {
                if (response.isSuccessful) {
                    val body = response.body()
                    if (body != null) {
                        if (body.isSuccess) {
                            val data = body.data
                            if (data != null) {
                                it.resume(data)
                            } else {
                                it.resumeWithException(Throwable("Response Body Contains No Data!"))
                            }
                        } else {
                            it.resumeWithException(
                                Throwable(
                                    "Https Error, error code: %d, error msg: %s".format(
                                        body.code,
                                        body.message
                                    )
                                )
                            )
                        }
                    } else {
                        it.resumeWithException(Throwable("Response Body Is Empty!"))
                    }
                } else {
                    it.resumeWithException(
                        Throwable(
                            response.message() ?: "Unknown Https Exception!"
                        )
                    )
                }
            }

            override fun onFailure(call: Call<WebResponseDto<T>>, t: Throwable) {
                it.resumeWithException(t)
            }

        })
    }
}

suspend fun Call<WebResponseDto<Any?>>.resumeWithSuccess(): Boolean {
    return suspendCoroutine {
        enqueue(object : Callback<WebResponseDto<Any?>> {

            override fun onResponse(
                call: Call<WebResponseDto<Any?>>,
                response: Response<WebResponseDto<Any?>>
            ) {
                if (response.isSuccessful) {
                    val body = response.body()
                    if (body != null) {
                        if (body.isSuccess) {
                            it.resume(true)
                        } else {
                            it.resume(false)
                        }
                    } else {
                        it.resumeWithException(Throwable("Response Body Is Empty!"))
                    }
                } else {
                    it.resumeWithException(
                        Throwable(
                            response.message() ?: "Unknown Https Exception!"
                        )
                    )
                }
            }

            /**
             * Invoked when a network exception occurred talking to the server or when an unexpected
             * exception occurred creating the request or processing the response.
             */
            override fun onFailure(call: Call<WebResponseDto<Any?>>, t: Throwable) {
                it.resumeWithException(t)
            }

        })
    }
}
